

#import "Question.h"
@interface Question()
{
    UIImage *m_image;
}

@end


@implementation Question

@synthesize  questionId, author, questionText, imageURL, date, votes, answerCount, reputation, gbadge, sbadge, bbadge, body, answers, userRequestURL,image;


-(UIImage*) image {
    return m_image;
}

-(id)initWithObj:(NSDictionary *)obj{
    self = [super init];
    NSDateFormatter *format = [[[NSDateFormatter alloc]init ]autorelease ];    
    [format setDateStyle:NSDateFormatterMediumStyle];
    [format setTimeStyle:NSDateFormatterShortStyle];
    self.questionId = [NSString stringWithFormat:@"%@",[obj objectForKey:@"question_id"]];
    self.imageURL = [NSURL URLWithString:[[obj objectForKey:@"owner"]objectForKey:@"profile_image"]];
    self.author = [[obj objectForKey:@"owner"]objectForKey:@"display_name"];
    self.questionText =[obj objectForKey:@"title"];
    self.date = [format stringFromDate:[NSDate dateWithTimeIntervalSince1970:[[obj objectForKey:@"creation_date"]doubleValue]]];
    self.votes = [NSString stringWithFormat:@"%@",[obj objectForKey:@"score"]];
    self.answerCount = [NSString stringWithFormat:@"%@",[obj objectForKey:@"answer_count"]];
    self.reputation = [NSString stringWithFormat:@"%@",[[obj objectForKey:@"owner"]objectForKey:@"reputation"]];
    self.body = [obj objectForKey:@"body"];
    self.answers =[obj objectForKey:@"answers"];
    self.userRequestURL = [[obj objectForKey:@"owner"]objectForKey:@"user_id"];
    return self;
}



+(Question*) questionWithObj:(NSDictionary*)obj{
    return [[[Question alloc]initWithObj:obj]autorelease];
}

+(Question*) setBadgesFor:(Question*)question source:(NSDictionary*)badges{
    question.gbadge = [NSString stringWithFormat:@"%@",[[badges objectForKey:@"badge_counts"]objectForKey:@"gold"]];
    question.sbadge = [NSString stringWithFormat:@"%@",[[badges objectForKey:@"badge_counts"]objectForKey:@"silver"]];
    question.bbadge = [NSString stringWithFormat:@"%@",[[badges objectForKey:@"badge_counts"]objectForKey:@"bronze"]];
    return question;
}

-(void) dealloc {
    [m_image release];
    self.imageURL = nil;
    self.questionId = nil;
    self.questionText = nil;
    self.author = nil;
    self.date = nil;
    self.votes = nil;
    self.answerCount = nil;
    self.reputation = nil;
    self.gbadge = nil;
    self.sbadge = nil;
    self.bbadge = nil;
    self.body = nil;
    self.answers = nil;
    self.userRequestURL = nil;
    [super dealloc];
}

- (NSDictionary *)dataForPage:(NSInteger)pageIndex
{
	return [[self.answers allObjects] objectAtIndex:pageIndex];
}

-(void) loadImage {
    
    [self performSelectorInBackground:@selector(loadImageInBackground)
                           withObject:nil];
}

-(void) loadImageInBackground {
    NSAutoreleasePool* pool = [NSAutoreleasePool new];
    
    NSURLRequest* request = [NSURLRequest requestWithURL:self.imageURL];
    NSError* error = nil;
    NSData* data =
    [NSURLConnection sendSynchronousRequest:request
                          returningResponse:nil error:&error];
    
    if ( error == nil ) {
        
        [m_image release];
        m_image = [[UIImage imageWithData:data] retain];
    
    }
    
    [pool release];
}

@end
